/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ingenieriaeconomica;

import java.text.DecimalFormat;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Lisi
 */
public class Intereses extends javax.swing.JFrame {

    /**
     * Creates new form Intereses
     */
    public Intereses() {
        initComponents();
    }

    private double[][] _data;
    private Principal Prim;
    public boolean _niv;
    private double A,J,M;
    private int P,N;
    private DecimalFormat FDecimal= new DecimalFormat("########.##");
        
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    
    private void Llenar()
    {   
        A=J=M=0.0;
        P=N=0;        
        A=Double.parseDouble(_ValorActual.getText());
        J=Double.parseDouble(_TasaInteres.getText())/100;
        M=Double.parseDouble(_NumeroCapitalizaciones.getText());
        switch(_PagosAño.getSelectedIndex())
        {
                case 0:{
                        P=1;
                        break;}
                case 1:{
                        P=2;
                        break;}
                case 2:{
                        P=3;
                        break;}
                case 3:{
                        P=4;
                        break;}
                case 4:{
                        P=6;
                        break;}
                case 5:{
                        P=12;
                        break;}
        }                
        N=Integer.parseInt(_Años.getText());
    }
    
    private void ModeloTablas()
    {        
        DefaultTableModel modelo= new DefaultTableModel();
        modelo.setRowCount(N);
        modelo.setColumnCount(2);
        DefaultTableModel modelo2= new DefaultTableModel();
        modelo2.setRowCount(N);
        modelo2.setColumnCount(2);
        _Nivelado.setModel(modelo);
        _NoNivelado.setModel(modelo2);
        
    }
    
    
    public void Frances()
    {
        double renta=(A*(Math.pow((1+(J/M)),(M/P))-1))/(1-Math.pow((1+(J/M)),(-M*N)));
        double interes=((Math.pow((1+(J/M)),(M/P))-1));
        double[] capital = new double[P*N];
        double[] amortizacion = new double[P*N];
        double[] interesMensual = new double[P*N];
        double[] interesAnual = new double[N];
        double cap=A;
        for(int i=0; i<P*N;i++)
        {
            capital[i]=cap;
            interesMensual[i]=cap*interes;
            amortizacion[i]=renta-interesMensual[i];              
            cap=capital[i]-amortizacion[i];
        }
        double SumInt=0;
        for(int i=0;i<N;i++)
        {
            SumInt=0;
            for(int j=0;j<P;j++)
            {
                SumInt+=interesMensual[j+(i*P)];                               
            }                 
            _Nivelado.setValueAt(i+1, i, 0);
            _data[0][i]=SumInt;
            _Nivelado.setValueAt(FDecimal.format(SumInt), i, 1);
        }
    }
    
    public void Aleman()
    {
        double amortizacion=A/(P*N);
        double interes=((Math.pow((1+(J/M)),(M/P))-1));
        double[] capital = new double[P*N];
        double[] interesMensual = new double[P*N];
        double[] interesAnual = new double[N];
        double cap=A;
        for(int i=0; i<P*N;i++)
        {
            capital[i]=cap;
            interesMensual[i]=cap*interes;             
            cap=capital[i]-amortizacion;
        }
        double SumInt=0;
        for(int i=0;i<N;i++)
        {
            SumInt=0;
            for(int j=0;j<P;j++)
            {
                SumInt+=interesMensual[j+(i*P)];                
            }           
            _NoNivelado.setValueAt(i+1, i, 0);
            _data[1][i]=SumInt;
            _NoNivelado.setValueAt(FDecimal.format(SumInt), i, 1);
        }
    }
    
    public void FormPri(Principal este)
    {
        Prim=este;
    }
    
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jScrollPane1 = new javax.swing.JScrollPane();
        _Nivelado = new javax.swing.JTable();
        jScrollPane2 = new javax.swing.JScrollPane();
        _NoNivelado = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        _Años = new javax.swing.JTextField();
        _ValorActual = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        _PagosAño = new javax.swing.JComboBox();
        jLabel3 = new javax.swing.JLabel();
        _NumeroCapitalizaciones = new javax.swing.JTextField();
        _TasaInteres = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jRadioButton1 = new javax.swing.JRadioButton();
        jRadioButton2 = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Modulo de Intereses");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        _Nivelado.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Año", "Interes"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Short.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane1.setViewportView(_Nivelado);

        _NoNivelado.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Año", "Interes"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Short.class, java.lang.Double.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(_NoNivelado);

        jButton1.setText("Calcular");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)), "Calculo de Intereses"));

        jLabel5.setText("n (Años)");

        _Años.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                _AñosFocusLost(evt);
            }
        });

        jLabel1.setText("A (Valor Actual)");

        jLabel4.setText("P (Numero de Pagos al Año)");

        _PagosAño.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "1 (Anual)", "2 (Semestral)", "3 (Tetramensual)", "4 (Trimensual)", "6 (Bimensual)", "12 (Mensual)" }));

        jLabel3.setText("m (Numero de Capitalizaciones)");

        jLabel2.setText("j (Tasa de Interes)");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel3)
                    .add(jLabel4)
                    .add(jLabel5)
                    .add(jLabel2)
                    .add(jLabel1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(_ValorActual)
                    .add(_TasaInteres)
                    .add(_Años)
                    .add(_PagosAño, 0, 1, Short.MAX_VALUE)
                    .add(_NumeroCapitalizaciones, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 95, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(_ValorActual, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(_TasaInteres, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(_NumeroCapitalizaciones, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(_PagosAño, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel5)
                    .add(_Años, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        buttonGroup1.add(jRadioButton1);
        jRadioButton1.setText("Nivelado (Frances)");
        jRadioButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton1ActionPerformed(evt);
            }
        });

        buttonGroup1.add(jRadioButton2);
        jRadioButton2.setText("No Nivelado (Aleman)");
        jRadioButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jRadioButton2ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 139, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jRadioButton1))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 142, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jRadioButton2))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 39, Short.MAX_VALUE))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jRadioButton1)
                            .add(jRadioButton2))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                            .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:
        Llenar();
        ModeloTablas();
        _data=new double[2][N];
        Frances();
        Aleman();        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void _AñosFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event__AñosFocusLost
        // TODO add your handling code here:
        jButton1.requestFocus();
    }//GEN-LAST:event__AñosFocusLost

    private void jRadioButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton1ActionPerformed
        // TODO add your handling code here:
        _niv=true;
    }//GEN-LAST:event_jRadioButton1ActionPerformed

    private void jRadioButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jRadioButton2ActionPerformed
        // TODO add your handling code here:
        _niv=false;
    }//GEN-LAST:event_jRadioButton2ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        int ele=1;
        if(_niv)
        {
            ele=0;            
        }
        for(int i=0;i<10;i++)
        {
            if(i<N)
            {
                Prim.LlenarTabla(FDecimal.format(_data[ele][i]), i+1);
            }
            else
            {
                Prim.LlenarTabla(0.0, i+1);
            }
        }
        
    }//GEN-LAST:event_formWindowClosing

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Intereses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Intereses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Intereses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Intereses.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Intereses().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField _Años;
    private javax.swing.JTable _Nivelado;
    private javax.swing.JTable _NoNivelado;
    private javax.swing.JTextField _NumeroCapitalizaciones;
    private javax.swing.JComboBox _PagosAño;
    private javax.swing.JTextField _TasaInteres;
    private javax.swing.JTextField _ValorActual;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JRadioButton jRadioButton1;
    private javax.swing.JRadioButton jRadioButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables
}
